// 用户自定义返佣设置
layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    form.on("submit(service)", function (data) {
        var id = $("#Id").val();
        var services = [];
        $(".service").each(function (index, item) {
            if (item.value) {
                var AgentTrade = $(item).parent().parent().find('.AgentTrade').val();
                var SalesTrade = $(item).parent().parent().find('.SalesTrade').val();
                services.push({
                    Id: $(item).attr("id"),
                    ProjectManagerId: id,
                    ProjectName: $(item).attr("chargeName"),
                    ProjectUnit: $(item).attr("chargeUnit"),
                    ProjectPrice: item.value,
                    AgentTrade: AgentTrade,
                    SalesTrade: SalesTrade,
                    ProjectCode: $(item).attr("name")
                });
            }
        });
        if (services.length) {
            $.ajax({
                type: 'POST',
                url: '/TradeCommission/SettingUserTrade/',
                data: {
                    Services: services
                },
                dataType: "json",
                headers: {
                    "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
                },
                success: function (res) {//res为相应体,function为回调函数
                    if (res.ResultCode === 200) {
                        //var alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                        //    layer.closeAll("iframe");
                        //    //刷新父页面
                        //    parent.location.reload();
                        //    top.layer.close(alertIndex);
                        //});
                        var alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                            location.reload();
                            top.layer.close(alertIndex);
                        });
                    }
                    else {
                        layer.alert(res.ResultMsg, { icon: 5 });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                }
            });
        } else {
            layer.alert('请添加您要设置的手续费！', { icon: 5 });
        }
        return false;
    });

    function validNumber(obj) {
        var _parent = $(obj).parent().parent();
        var _service = _parent.find('.service');
        var _service1 = _parent.find('.service1').val();
        var _service2 = _parent.find('.service2').val();
        //var trade = _service.attr('trade');
        //var charge = _service.attr('charge');
        var tag = _service.attr('tag');
        var val = 0;

        if (_service1 && _service2) {
            if (isNaN(_service1)) {
                _service.val(0);
                layer.alert(tag + '[佣金]必须为数字！');
                return;
            }
            if (isNaN(_service2)) {
                _service.val(0);
                layer.alert(tag + "[成本]必须为数字！");
                return;
            }
            var val1 = parseFloat(_service1);
            var val2 = parseFloat(_service2);
            //val = val1 + val2;
            //if (val > charge) {
            //    _service.val(0);
            //    layer.alert(tag + '手续费不能高于[总手续费]！');
            //    return;
            //}
            //if (val < trade) {
            //    _service.val(0);
            //    layer.alert(tag + '手续费不能低于[标准手续费]！');
            //    return;
            //}
            if (val1 < 0) {
                layer.alert(tag + '手续费必须大于零！');
                return;
            }
            if (val2 < 0) {
                layer.alert(tag + '手续费必须大于零！');
                return;
            }
            _service.val(val);
        }
    }

    $(".service1, .service2").blur(function () {
        validNumber(this);
    });

    $('.hisstroy').click(function () {
        var key = $(this).attr('key');
        var code = $(this).attr('code');

        if (key && code) {
            $.ajax({
                type: 'GET',
                url: '/ServiceCharge/GetHistoryByManagerId/',
                data: {
                    ManagerId: key,
                    ProjectCode: code,
                },
                dataType: "json",
                success: function (res) {//res为相应体,function为回调函数
                    console.log(res);
                    var msg = [];
                    for (var i = 0; i < res.length; i++) {
                        var item = res[i];
                        msg.push('品种名称(代码,货币)：' + item.ProjectName + '(' + item.ProjectCode +','+ item.ProjectUnit + ')' + '\t佣金：' + item.AgentTrade + '\t成本：' + item.SalesTrade + '\t时间：' + item.AddTime);
                    }
                    layer.alert(msg.join('<br />'), { title:'历史修改记录',  area: '600px' });

                    //if (res.ResultCode === 200) {

                    //    //var alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                    //    //    layer.closeAll("iframe");
                    //    //    //刷新父页面
                    //    //    parent.location.reload();
                    //    //    top.layer.close(alertIndex);
                    //    //});
                    //}
                    //else {
                    //    layer.alert(res.ResultMsg, { icon: 5 });
                    //}
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                }
            });
        } else {
            layer.msg('没有历史记录！');
        }
        console.log(key);
        console.log(code);
    });

    form.verify({
        service: function (value, item) { //value：表单的值、item：表单的dom对象
            //validNumber(item);
            if (value) {
                if (isNaN(value)) {
                    return '只能录入数字！';
                }
                var tag = $(item).attr('tag');
                var val = parseFloat(value);
                //var trade = parseFloat($(item).attr('trade'));
                //var charge = parseFloat($(item).attr('charge'));
                //if (val > charge) {
                //    return tag + '手续费不能高于总手续费！';
                //}
                //if (val < trade) {
                //    return tag + '手续费不能低于标准手续费！';
                //}
                if (val < 0) {
                    return tag + '手续费必须大于零！';
                }
            }
        },
        service1: function (value, item) {
            //var res = validNumber(item);
            //if (res) {
            //    return res;
            //}
        },
        service2: function (value, item) {
            //var res = validNumber(item);
            //if (res) {
            //    return res;
            //}
        }
    });


});